<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		getEditor,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from '@/static/js/moment.js'
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				usercCache:"hr-u-tempInstAdjust-user-cache",
				newInstCache:"hr-u-tempInstAdjust-newInst-cache",
				posList:[],
				origHandleWayList:[],
				newPosId:"",
				changeTextArea:false
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				let flag = false
				let hasSalary = false
				for(let item of _that.subEditorObj['title_new'].workList){
					if(item.newWorkTypeCode == '5176'){
						flag = true
					}
					if(item.newSalaryPosStr){
						hasSalary = true
						item.newSalaryPos = true
					}
				}
				// if(!hasSalary){
				// 	let groutitle = _that.$refs.autoform.getGroupTitle('title_new')
				// 	for(let item of _that.subConfigData["title_new"][0]){
				// 		if(item.editors){
							
				// 		}
				// 	}
				// }else{
					_that.formdata.objStr2 = JSON.stringify(_that.subEditorObj["title_new"].workList)
					if(flag){
						confirmModal(_that,'提示信息','任职的岗位中有见习的任职方式，是否需要填写试用期？点击“确认”继续编辑，点击“取消”继续保存。',()=>{},()=>{
							if(_that.checkedit){
								if(_that.subEditorObj["title_new"].workList.length<1){
									confirmModal(_that,'提示信息','未设置新任职岗位，是否继续保存',()=>{
										_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
									},()=>{
									})
								}else{
									_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
								}
							}else{
								if(_that.subEditorObj["title_new"].workList.length<1){
									confirmModal(_that,'提示信息','未设置新任职岗位，是否继续保存',()=>{
										_that.om.formSave("tempInstAdjust/addTempInstAdjust",_that.formdata,_that)
									},()=>{
									})
								}else{
									_that.om.formSave("tempInstAdjust/addTempInstAdjust",_that.formdata,_that)
								}
							}
						})
					}else{
						if(_that.checkedit){
							if(_that.subEditorObj["title_new"].workList.length<1){
								confirmModal(_that,'提示信息','未设置新任职岗位，是否继续保存',()=>{
									_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
								},()=>{
								})
							}else{
								_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
							}
						}else{
							if(_that.subEditorObj["title_new"].workList.length<1){
								confirmModal(_that,'提示信息','未设置新任职岗位，是否继续保存',()=>{
									_that.om.formSave("tempInstAdjust/addTempInstAdjust",_that.formdata,_that)
								},()=>{
								})
							}else{
								_that.om.formSave("tempInstAdjust/addTempInstAdjust",_that.formdata,_that)
							}
						}
					}
				// }
			},
			verifyUser(d){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				_that.om.request(
					"tempInstAdjust/verifyUser",
					{
						userId:d.id
					}
				).then(res=>{
					if(res.value.one){
						errorModal(_that,"提示信息",ignoreHtml(res.value.one),()=>{})
					}else if(res.value.two){
						confirmModal(_that,"提示信息",ignoreHtml(res.value.two),()=>{
							formdata.userId = d.id
							formdata.userName = d.username
							_that.setInfo(d.id)
							_that.getListByCode()
							_that.getOrgList()
							_that.onSelectOrg()
							formdata.sexName = d.sexName
							formdata.birthdate = d.birthdate
							formdata.birthPlace = d.birthPlace
							formdata.entrydate = d.entrydate
							formdata.areaName = d.areaName
							formdata.idTypeName = d.idTypeName
							formdata.idcard = d.idcard
							formdata.certificateValidDate = d.certificateValidDate
							formdata.nationName = d.nationName
							formdata.marriageName = d.marriageName
							formdata.politicalName = d.politicalName
							formdata.bloodTypeName = d.bloodTypeName
							formdata.userno = d.userno
							formdata.registerCategoryName = d.registerCategoryName
							formdata.birthaddress = d.birthaddress
							formdata.currentaddress = d.currentaddress
							formdata.salarycatName = d.salarycatName
							formdata.salaryPosInstName = d.salaryPosInstName
							formdata.customCateAName  = d.customCateAName
							formdata.attributeName = d.attributeName
							formdata.roleName = d.roleCategoryName
							_that.getSalaryList(d.id)
						},()=>{})
					}else{
						formdata.userId = d.id
						formdata.userName = d.username
						_that.setInfo(d.id)
						_that.getListByCode()
						_that.getOrgList()
						_that.onSelectOrg()
						formdata.sexName = d.sexName
						formdata.birthdate = d.birthdate
						formdata.birthPlace = d.birthPlace
						formdata.entrydate = d.entrydate
						formdata.areaName = d.areaName
						formdata.idTypeName = d.idTypeName
						formdata.idcard = d.idcard
						formdata.certificateValidDate = d.certificateValidDate
						formdata.nationName = d.nationName
						formdata.marriageName = d.marriageName
						formdata.politicalName = d.politicalName
						formdata.bloodTypeName = d.bloodTypeName
						formdata.userno = d.userno
						formdata.registerCategoryName = d.registerCategoryName
						formdata.birthaddress = d.birthaddress
						formdata.currentaddress = d.currentaddress
						formdata.salarycatName = d.salarycatName
						formdata.salaryPosInstName = d.salaryPosInstName
						formdata.customCateAName  = d.customCateAName
						formdata.attributeName = d.attributeName
						formdata.roleName = d.roleCategoryName
						_that.getSalaryList(d.id)
					}
				})
			},
			getSalaryList(id,posId){
				let _that = this
				_that.om.request(
					"tempInstAdjust/getSalaryList1",
					{
						userId: id,
						busCode:_that.busCode,
						posId:posId?posId:''
					}
				).then(res=>{
					if(_that.subEditorObj["title_new"].workList.length){
						for(let item of _that.subEditorObj["title_new"].workList){
							if(item.newSalaryPosStr){
								_that.changeTextArea = true
							}else{
								_that.changeTextArea = false
							}
						}
					}
					for(let item of res.value){
						if(item.salaryName != 'salary1' && item.salaryName != 'salary2' && item.salaryName != 'salary3' && item.salaryName != 'salary4'){
							item.showSubTitle2 = false
						}
					}
					if(_that.subEditorObj["title_salary"]){
						_that.subEditorObj["title_salary"].workList = res.value
					}
				})
			},
			getTipAfterUser(id){
				let _that = this
				_that.om.request(
					"hrPosUtil/getTipAfterUser",
					{
						instId:id,
						formCode: _that.busCode ||'HRS0125',
						userId:_that.$refs.autoform.formdata.userId
					}
				).then(res=>{
					if(res.value.attributes.pos){
						_that.posList = res.value.attributes.pos
					}
				})
			},
			verifyTitle_new(){
				let _that = this
				if(!_that.$refs.autoform.formdata.newInstId){
					let title = _that.$refs.autoform.getEditorTitle("title_inst","newInstId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			getWorkParam(){
				let _that = this
				let arr = []
				let arr2 = []
				if(_that.subEditorObj["title_orig"]){
					for(let item of _that.subEditorObj["title_orig"].workList){
						if(item.origHandleWayCode != '35516'){
							arr.push(item.origPosId)
							arr2.push(item)
						}
					}
				}
				let title = _that.$refs.autoform.getEditorTitle("title_user","entrydate")
				return {
					instId:_that.$refs.autoform.formdata.newInstId,
					posStr:JSON.stringify(_that.posList),
					posIdList:arr,
					userId:_that.$refs.autoform.formdata.userId,
					userName:_that.$refs.autoform.formdata.userName,
					origHandleWayList:JSON.stringify(_that.origHandleWayList),
					posList:JSON.stringify(arr2),
					edit:_that.edit,
					entrydate:_that.$refs.autoform.formdata.entrydate,
					title:title
				}
			},
			getWorkParam1(){
				let _that = this
				return {
					origHandleWayList:JSON.stringify(_that.origHandleWayList),
					userId:_that.$refs.autoform.formdata.userId
				}
			},
			getWorkParam2(){
				let _that = this
				return {
					changeTextArea:_that.changeTextArea,
					newPosId:_that.newPosId,
					isEdit:(_that.edit== true?true:false),
					userId:_that.$refs.autoform.formdata.userId?_that.$refs.autoform.formdata.userId:''
				}
			},
			getInstInfo(id){
				let _that = this
				_that.om.request(
					"tempInstAdjust/getInstInfo",
					{
						instId:id
					}
				).then(res=>{
					if(res.value){
						_that.$refs.autoform.formdata.instMainUserName = res.value.instMainUserName
						_that.$refs.autoform.formdata.parentInstName = res.value.parentInstName
					}
				})
			},
			verifyInst(id){
				let _that = this
				_that.om.request(
					"tempInstAdjust/verifyInst",{
						instId:id
					}
				).then(res=>{
					if(res.succeed && res.value){
						errorModal(_that,"提示信息",ignoreHtml(res.value),()=>{
							_that.$refs.autoform.clear("title_inst","newInstId")
							_that.$refs.autoform.clear("title_inst","instMainUserName")
							_that.$refs.autoform.clear("title_inst","parentInstName")
							_that.$refs.autoform.clear("title_inst","instLevelId")
						})
					}else{
						_that.getTipAfterUser(id)
						_that.getInstInfo(id)
						_that.om.request(
							"tinstitution/getInstInfo",
							{
								instId:id
							}
						).then(res=>{
							_that.$refs.autoform.formdata.instLevelId =  res.value.instcatId
							_that.$refs.autoform.formdata.instLevelName =  res.value.instcatName
						})
					}
				})
			},
			onSelectOrg(item){
				let _that = this
				_that.$refs.autoform.changeEditor("title_inst","newInstId","type","linkbox")
				_that.$refs.autoform.changeEditor("title_inst","newInstId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							orgId:(item?item.id:_that.orgId)
						}),
						url:"tempInstAdjust/findNotAllAndSelf",
						namefield:"text",
						cachename:_that.newInstCache,
						initload:true,
						placeholder:"请输入机构机构名称",
					}
				})
			},
			getOrgList(){
				let _that = this
				_that.om.request(
					"tempInstAdjust/getOrgList",
					{
						zuserId:_that.userId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_inst","newOrgId","type","selectbox")
					_that.$refs.autoform.changeEditor("title_inst","newOrgId","data",configSelectData(res,"id",'shortname'))
					_that.$refs.autoform.initSelectIndex("title_inst","newOrgId")
					_that.$refs.autoform.changeEditor("title_inst","newOrgId","onSelect","onSelectOrg")
				})
			},
			getListByCode(){
				let _that = this
				_that.om.request(
					"tsysdict/getListByCode",
					{
						code:'38795'	
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_inst","adjustTypeId","data",configSelectData(res,"id","dicname"))
					_that.$refs.autoform.initSelectIndex("title_inst","adjustTypeId")
				})
			},
			setInfo(id){
				let _that = this
				_that.om.request(
					"tempInstAdjust/setInfo",
					{
						userId:id
					}
				).then(res=>{
					if(res.value.temployeeResponse){
						_that.$refs.autoform.formdata.mobile = res.value.temployeeResponse.mobile
						_that.$refs.autoform.formdata.highestEduName = res.value.temployeeResponse.highestEduName
						_that.subEditorObj["title_orig"].workList = res.value.origs
						_that.subEditorObj["title_handle"].workList = res.value.handles
					}
				})
			},
			initLoad(options){
				let _that = this 
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"dictZero/getListByCode",
						param:{
							code: '35510',
							inCode: ['35511','35516','37697']
						}
					},{
						url:"wxPosBusiAuth/defaultTableMaker",
						param: {
							code: options.code
						},
						method: "post"
					}
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:'commonForm/getInfo',
						param:{
							fileId: fileId,
							formCode: options.code,
							lookAuth: '',
							validateType: 'CHECK_EDIT'
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					var defaultPosId = res[2].value.id || '';
					var defaultPosName = res[2].value.posName || '';
					var defaultInstId = res[2].value.instId || '';
					var defaultInstName = res[2].value.instName || '';
					// if(!res[0].length && !_that.checkedit){
					// 	errorModal(_that,'提示信息','无权限编辑',()=>{
					// 		uni.navigateBack({
					// 		});
					// 	})
					// }
					_that.origHandleWayList = res[1]
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("title_user","userId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify(),
							url:"tempPosAdjust/getComboboxAll",
							cachename:_that.usercCache,
							initload:true,
							searchfield:"q",
							namefield:'userSalaryPosInstName',
							extrafield:"salaryPosInstName",
							placeholder:"请输入姓名"
						}
					},null,mainData)
					if(_that.subEditorObj["title_orig"]){
						let att = {
							showAdd:false,
							showDel:false,
							getWorkParam:"getWorkParam1"
						}
						let configData = _that.subConfigData["title_orig"]
						if(hasEditor("title_orig","origPosId",configData)){
							att.titleTemplate = "<%= origPosName || '未填写'%>"
						}
						changeEditor("title_orig","objStr1",att,mainData)
					}
					if(_that.subEditorObj["title_new"]){
						let att = {
							getWorkParam:"getWorkParam"
						}
						let configData = _that.subConfigData["title_new"]
						if(hasEditor("title_new","newReasonCode",configData)){
							let title = getEditorTitle("title_new","newReasonCode",configData)
							att.titleTemplate = "<%= newReasonName || '未填写'%>"
						}
						changeEditor("title_new","objStr2",att,mainData)
					}
					if(_that.subEditorObj["title_salary"]){
						let att = {
							showAdd:false,
							showDel:false,
							getWorkParam:'getWorkParam2'
						}
						let configData = _that.subConfigData["title_salary"]
						if(hasEditor("title_salary","itemName",configData)){
							att.titleTemplate = "<%= itemName || '未填写'%>"
						}
						if(hasEditor("title_salary","beforeAdjustId",configData)){
							let title = getEditorTitle("title_salary","beforeAdjustId",configData)
							att.subtitle1Template = title + ":<%= beforeAdjust || '无'%>"
						}
						if(hasEditor("title_salary","afterAdjustId",configData)){
							let title = getEditorTitle("title_salary","afterAdjustId",configData)
							if(att.subtitle1Template){
								att.subtitle1Template = att.subtitle1Template +' | '+title + ":<%= afterAdjust || '未填写'%>"
							}else{
								att.subtitle1Template =title + ":<%= afterAdjust || '未填写'%>"
							}
						}
						if(hasEditor("title_salary","salary",configData)){
							let title = getEditorTitle("title_salary","salary",configData)
							att.subtitle2Template = title + ":<%= salary || '未填写'%>"
						}
						if(hasEditor("title_salary","remark",configData)){
							let title = getEditorTitle("title_salary","remark",configData)
							att.subtitle3Template = title + ":<%= remark || '未填写'%>"
						}
						changeEditor("title_salary","objStr3",att,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'adjustPosFlag',res[3].value.adjustPosFlag)
						_that.$set(_that.formdata,'adjustPosFlag',res[3].value.adjustPosFlag)
						for(let index2 in res[3].value){
							if(fieldArr.includes(index2)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index2,res[3].value[index2])
								_that.$set(_that.formdata,index2,res[3].value[index2])
							}
						}
						for(let index1 in res[3].value.temployeeResponse){
							if(fieldArr.includes(index1)){
								if(index1 == 'id'){
									continue
								}
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index1,res[3].value.temployeeResponse[index1])
								_that.$set(_that.formdata,index1,res[3].value.temployeeResponse[index1])
							}
						}
						for(let index in res[3].value.tempInstAdjust){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[3].value.tempInstAdjust[index])
								_that.$set(_that.formdata,index,res[3].value.tempInstAdjust[index])
							}
						}
						if(_that.subEditorObj["title_orig"]){
							let origList = _that._.cloneDeep(res[3].value.origs)
							 _that.subEditorObj["title_orig"].workList = origList
							 _that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr1",JSON.stringify(origList))
							_that.$set(_that.formdata,"objStr1",JSON.stringify(origList))
						}
						if(_that.subEditorObj["title_new"]){
							let newList = _that._.cloneDeep(res[3].value.news)
							 _that.subEditorObj["title_new"].workList = newList
							 _that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr2",JSON.stringify(newList))
							_that.$set(_that.formdata,"objStr2",JSON.stringify(newList))
						}
						if(_that.subEditorObj["title_salary"]){
							let salaryList = _that._.cloneDeep(res[3].value.salaries)
							_that.subEditorObj["title_salary"].workList = salaryList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr3",JSON.stringify(salaryList))
							_that.$set(_that.formdata,"objStr3",JSON.stringify(salaryList))
						}
						_that.getListByCode()
						if(_that.formdata.newInstId){
							changeEditor("title_inst","newInstId","type","linkbox",mainData)
							changeEditor("title_inst","newInstId",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({
										orgId:(_that.formdata.newOrgId?_that.formdata.newOrgId:_that.orgId)
									}),
									url:"tinstitution/findNotAll",
									namefield:"instname",
									cachename:_that.newInstCache,
									initload:true,
									placeholder:"请输入机构机构名称",
								}
							},null,mainData)
						}
						let configData = _that.subConfigData["title_salary"]
						let editor = getEditor("title_salary","afterAdjustId",configData)
						if(editor.checkedit){
							let newPosId = ''
							if(_that.subEditorObj['title_new'] && _that.subEditorObj['title_new'].workList.length>0){
								for(let item of _that.subEditorObj["title_new"].workList){
									if(item.newSalaryPos){
										newPosId = item.newPosId
									}
								}
							}
							for(let salaryItem of _that.subEditorObj["title_salary"].workList){
								if(salaryItem.afterAdjustId){
									return
								}
							}
							_that.getSalaryList(_that.$refs.autoform.formdata.userId,newPosId)
						}
						if(_that.$refs.autoform.formdata.newInstId){
							_that.verifyInst(_that.$refs.autoform.formdata.newInstId)
						}
					}
					_that.$nextTick(function(){
						// if(!_that.edit && !_that.checkedit){
						// 	_that.$refs.autoform.initSelectVal("topinfo","zposId")
						// }
						
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.formdata.zposId = defaultPosId
							_that.$refs.autoform.formdata.zposName = defaultPosName
							_that.$refs.autoform.formdata.zinstId = defaultInstId
							_that.$refs.autoform.formdata.zinstName = defaultInstName
							_that.$nextTick(function() {
								_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
							})
						}else{
							_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
						}
						
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							editor.workList = res.data
							if(fieldId =="title_new"){
								let hasNew = _that.subEditorObj["title_new"].workList.find((item,index,arr)=>{
									return  item.newSalaryPosStr == true
								})
								if(hasNew){
									_that.getSalaryList(_that.$refs.autoform.formdata.userId,hasNew.newPosId)
									_that.newPosId = hasNew.newPosId
								}else{
									_that.getSalaryList(_that.$refs.autoform.formdata.userId)
								}
							}
							if(fieldId =="title_salary"){ 
								let dataList = JSON.parse(JSON.stringify(_that.subEditorObj["title_salary"].workList))
									let num = ""
									for(let item of dataList){
										if(item.salaryName == "salary1"){
											num = Number(item.afterAdjust)
										}else if(item.salaryName =="salary2"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary3"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary4"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary5"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary6"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary7"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary8"){
											num = num - Number(item.afterAdjust)
										}else if(item.salaryName =="salary9"){
											item.afterAdjust = num
										}
									}
									_that.subEditorObj['title_salary'].workList = dataList
								}
								if(fieldId =="title_orig"){ 
									for(let item of _that.subEditorObj["title_orig"].workList){
										if(item.isChanged){
											_that.subEditorObj['title_new'].workList = []
											item.isChanged = false
										}
									}
								}
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
				uni.getStorage({
					key:_that.newInstCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newInstName = d.text || d.instName
							formdata.newInstId = d.id
							if(_that.subEditorObj["title_new"]){
								_that.subEditorObj["title_new"].workList = []
							}
							_that.verifyInst(d.id)
							uni.removeStorage({
								key:_that.newInstCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.usercCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.verifyUser(d)
							uni.removeStorage({
								key:_that.usercCache
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
